﻿using System.ComponentModel;
using System.Linq;
using System.Windows.Forms;
using PolePosition.v3.EntityLibrary;

namespace PolePosition.v3.Desktop.Editors.Controls
{
    public partial class Sectionalizer : DialogEditBase
    {
        public Sectionalizer()
        {
            InitializeComponent();
            BindCombos();
        }

        public Common.Enumerations.DataTypes DataType { get; private set; }
        private ProjectCommon2.Generics.EntityCollection<VendorDistributionNotesXSectionalizerEntity> m_NotexXSectionalizer;

        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public object Data
        {
            get
            {
                return bindingSourceSectionalizer.DataSource;
            }
            set
            {
                bindingSourceSectionalizer.SuspendBinding();
                SetupDatabindings(value);
                bindingSourceSectionalizer.DataSource = value;
                bindingSourceSectionalizer.ResumeBinding();
            }
        }

        private void BindCombos()
        {
            if (Program.LookupTableData != null)
            {
                ComboOptions.DistributionLookUp(comboBoxPhase, VendorDistributionLookupsType.Phase);
                ComboOptions.DistributionLookUp(comboBoxPCBType, VendorDistributionLookupsType.PCBType);
                ComboOptions.DistributionLookUp(comboBoxDeterminedBy, VendorDistributionLookupsType.DeterminedBy);
                ComboOptions.DistributionLookUp(comboBoxInterrupter, VendorDistributionLookupsType.Interrupter);
                ComboOptions.Range(comboBoxCurveA, 0, 9);
                ComboOptions.Range(comboBoxCurveB, 0, 9);
                ComboOptions.Range(comboBoxCurveC, 0, 9);
                ComboOptions.Range(comboBoxCurveD, 0, 9);

                listSelectorNotes.DisplayMember = VendorDistributionLookupsEnumerations.Columns.LookUpText.ToString();
                listSelectorNotes.ValueMember = VendorDistributionLookupsEnumerations.Columns.Vendor_Distribution_LookupId.ToString();
                
                if (EditAllowed)
                    listSelectorNotes.DataSource = Program.LookupTableData.VendorDistributionLookups.Where(x => x.LookUpType == (int)VendorDistributionLookupsType.Keywords).ToList();
                else
                    listSelectorNotes.Clear();
            }
        }

        [DefaultValue(false)]
        private bool m_EditAllowed;
        public bool EditAllowed
        {
            get
            {
                return m_EditAllowed;
            }
            set
            {
                m_EditAllowed = value;
            }
        }

        private void SetupDatabindings(object data)
        {
            if (data.GetType() == typeof(VendorDistributionSectionalizerEntity))
            {
                DataType = Common.Enumerations.DataTypes.ProjectsDistributionInspection;

                bindingSourceSectionalizer.DataSource = typeof(VendorDistributionSectionalizerEntity);

                LabelCreatedDate.DataBindings.Add(new Binding("Text", bindingSourceSectionalizer, "CreatedDate", true, DataSourceUpdateMode.OnPropertyChanged));
                textBoxLatitude.DataBindings.Add(new Binding("Text", bindingSourceSectionalizer, "Latitude", true, DataSourceUpdateMode.OnPropertyChanged));
                textBoxLongitude.DataBindings.Add(new Binding("Text", bindingSourceSectionalizer, "Longitude", true, DataSourceUpdateMode.OnPropertyChanged));
                textBoxSerialNumber.DataBindings.Add(new Binding("Text", bindingSourceSectionalizer, "SerialNumber", true, DataSourceUpdateMode.OnPropertyChanged));
                textBoxManufacturer.DataBindings.Add(new Binding("Text", bindingSourceSectionalizer, "Manufacturer", true, DataSourceUpdateMode.OnPropertyChanged));
                txtManufDate.DataBindings.Add(new Binding("Text", bindingSourceSectionalizer, "ManufDateFormatted", true, DataSourceUpdateMode.OnPropertyChanged));
                textBoxPoleNumber.DataBindings.Add(new Binding("Text", bindingSourceSectionalizer, "PoleNumber", true, DataSourceUpdateMode.OnPropertyChanged));
                textBoxMaxkV.DataBindings.Add(new Binding("Text", bindingSourceSectionalizer, "MaxkV", true, DataSourceUpdateMode.OnPropertyChanged));
                textBoxType.DataBindings.Add(new Binding("Text", bindingSourceSectionalizer, "Type", true, DataSourceUpdateMode.OnPropertyChanged));

                comboBoxPhase.DataBindings.Add(new Binding("SelectedValue", bindingSourceSectionalizer, "PhaseFk", true, DataSourceUpdateMode.OnPropertyChanged));
                comboBoxInterrupter.DataBindings.Add(new Binding("SelectedValue", bindingSourceSectionalizer, "Interrupter", true, DataSourceUpdateMode.OnPropertyChanged));
                comboBoxCurveA.DataBindings.Add(new Binding("SelectedText", bindingSourceSectionalizer, "CurveA", true, DataSourceUpdateMode.OnPropertyChanged));
                comboBoxCurveB.DataBindings.Add(new Binding("SelectedText", bindingSourceSectionalizer, "CurveB", true, DataSourceUpdateMode.OnPropertyChanged));
                comboBoxCurveC.DataBindings.Add(new Binding("SelectedText", bindingSourceSectionalizer, "CurveC", true, DataSourceUpdateMode.OnPropertyChanged));
                comboBoxCurveD.DataBindings.Add(new Binding("SelectedText", bindingSourceSectionalizer, "CurveD", true, DataSourceUpdateMode.OnPropertyChanged));

                textBoxComments.DataBindings.Add(new Binding("Text", bindingSourceSectionalizer, "Comments", true, DataSourceUpdateMode.OnPropertyChanged));
                textBoxPPM.DataBindings.Add(new Binding("Text", bindingSourceSectionalizer, "PPM", true, DataSourceUpdateMode.OnPropertyChanged));
                txtTestNo.DataBindings.Add(new Binding("Text", bindingSourceSectionalizer, "TestNo", true, DataSourceUpdateMode.OnPropertyChanged));
                
                comboBoxPCBType.DataBindings.Add(new Binding("SelectedValue", bindingSourceSectionalizer, "PCBType", true, DataSourceUpdateMode.OnPropertyChanged));
                comboBoxDeterminedBy.DataBindings.Add(new Binding("SelectedValue", bindingSourceSectionalizer, "DeterminedByFk", true, DataSourceUpdateMode.OnPropertyChanged));

                checkBoxPre1978.DataBindings.Add(new Binding("Checked", bindingSourceSectionalizer, "Pre1978", true, DataSourceUpdateMode.OnPropertyChanged));
                chkSticker.DataBindings.Add(new Binding("Checked", bindingSourceSectionalizer, "Sticker", true, DataSourceUpdateMode.OnPropertyChanged));

                LoadDataAsynch((data as PolePosition.v3.EntityLibrary.VendorDistributionSectionalizerEntity).Vendor_Distribution_Sectionalizers_SectionalizerId.Value);
            }
        }

        private void LoadDataAsynch(int sectionalizerId)
        {
            listSelectorNotes.LoadingVisible = true;
            backgroundWorkerNotes.RunWorkerAsync(sectionalizerId);
        }

        private void backgroundWorkerNotes_DoWork(object sender, DoWorkEventArgs e)
        {
            m_NotexXSectionalizer = ServiceProxy.UtilityService.GetDistributionInspectionProjectNotesForSectionalizer((int)e.Argument);

            if (backgroundWorkerNotes.CancellationPending)
                e.Cancel = true;
        }

        private void backgroundWorkerNotes_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Cancelled)
                return;

            listSelectorNotes.LoadingVisible = false;

            if (e.Error != null)
            {
                DevComponents.DotNetBar.MessageBoxEx.Show("A Data Retrieval Error has occurred attempting to load the sectionalizer notes. Please close editor and try again.", "Data Retrieval Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                listSelectorNotes.Enabled = false;
                return;
            }

            if (DataType == Common.Enumerations.DataTypes.ProjectsDistributionInspection)
            {
                if (m_NotexXSectionalizer != null)
                {
                    if (EditAllowed)
                    {
                        var _NotesValues = from note in m_NotexXSectionalizer
                                           select note.Vendor_Distribution_NoteFk.Value;
                        listSelectorNotes.SetSelectedValues(_NotesValues.ToList());
                    }
                    else
                    {
                        if (m_NotexXSectionalizer.Count() > 0)
                        {

                            //this will only show selected items
                            var _NotesValues = from note in m_NotexXSectionalizer
                                               join lookupdata in Program.LookupTableData.VendorDistributionLookups
                                               on note.Vendor_Distribution_NoteFk equals lookupdata.Vendor_Distribution_LookupId
                                               select lookupdata;

                            listSelectorNotes.ReadOnlyNoneSelectedVisible = false;
                            listSelectorNotes.DataSource = _NotesValues.ToList();
                        }
                        else
                        {
                            listSelectorNotes.ReadOnlyNoneSelectedVisible = true;
                        }
                    }
                }

                listSelectorNotes.Enabled = true;
            }
        }


    }
}
